public class Visitor extends Thread
{
    private Museum museum;

    private static final long VISITOR_AVG_VISIT_TIME = 2000L;
    private static final long VISITOR_MIN_VISIT_TIME = 1000L;

    public Visitor(Museum museum) { this.museum = museum; }

    private void handleSleep(long millis)
    {
        try {
            this.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void run()
    {
        System.out.println(this.getId() + " wants to enter the museum");
        museum.enter();
        System.out.println("\t\t" + this.getId() + " has entered the gallery");
        handleSleep(((long) Math.random()) * VISITOR_AVG_VISIT_TIME + VISITOR_MIN_VISIT_TIME);
        museum.leave();
        System.out.println("\t\t\t" + this.getId() + " has left the museum");
    }
}
